import { Component, OnDestroy } from '@angular/core';

import { Util } from '../../utils/util';

import { Subscription } from 'rxjs';


export class ShopBase {
  // busy
  busy: Subscription;

  user:any;
  // data
  vm: any = {
    page: 1,
    table: {
      shops: [],
      count: 0
    }
  };

  // 实现数据绑定
  shopType: string = "";

  // 参数
  param: any = {
    start: 0,
    rows: 10,
    sort: 'sellMoney',
    sortType: 'desc'
  };

  // http service
  service:any;

  constructor(public util: Util, Serivce) {

    this.user = this.util.sessionStore().get('user');

    this.util.sortHandel(this.param);

    this.service = Serivce;
  }



  // 列
  public columns: Array<any> = [
    {
      title: '店铺名称',
      name: 'brandName',
      sort: false
    },
    {
      title: '主营类目',
      name: 'brandName',
      sort: false
    },
    {
      title: '店铺等级',
      name: 'shopLevel',
      class:'levelTh',
      sort: true
    },
    {
      title: '动销宝贝数',
      name: 'productCount',
      sort: true
    },
    {
      title: '日销售额',
      name: 'sellMoney',
      sort: true
    },
    {
      title: '日销售量',
      name: 'sellAmount',
      sort: true
    },
    {
      title: '近30天销售额',
      name: 'monthSellMoney',
      sort: true
    },
    {
      title: '近30天销售量',
      name: 'monthSellAmount',
      sort: true
    }
  ];


  ngOnDestroy() {
    if(this.busy){
      this.busy.unsubscribe();
    }
    
  }

  // st-sort directive emit
  sortChanged(sort) {
    console.log('xxx');
    console.log(sort);
    this.getHttp(this.param);
  }

  //分页
  pageChanged(currentPage) {
    console.log(currentPage);


    this.vm.page = currentPage;

    this.param.start = (currentPage - 1) * this.param.rows;

    this.getHttp(this.param);

  }

  catStore: any;
  // out handel
  handleUpdated(cat) {


    if (!this.util.equals(this.catStore, cat)) {

      this.catStore = Object.assign({},cat);

      this.param.mainCat = cat.wholeName;

      console.log('cat $watch');

      this.getHttp(this.param);
    }

  }

  // shopType Change
  shopTypeChange(data) {

    console.log(data);

    // 第一页
    this.pageChanged(1);

  }



  getHttp(param) {

    this.busy = this.service.getShop(param).subscribe((res: any) => {

      for (let i in res.data.value) {
        let item = res.data.value[i];

        item = this.util.shopLevels(item);
      }

      this.vm.table.shops = res.data.value;

      this.vm.table.count = res.data.count;

    }, (err: any) => {

    });
  }

}
